/**
 * Created by yuris on 12.03.15.
 */
package InterfaceObjects {
import com.greensock.TweenMax;
import com.greensock.easing.Back;
import com.greensock.easing.Quad;

import feathers.display.Scale9Image;
import feathers.textures.Scale9Textures;

import flash.geom.Point;

import flash.geom.Rectangle;

import starling.display.Sprite;
import starling.events.Event;

public class WallObjectContextMenu extends Sprite {
    private var _sellButton:ButtonSmall;
    private var _moveButton:ButtonSmall;

    public static const EVENT_CONTEXT_BUTTON_TRIGGERED:String = "EVENT_WALL_OBJECT_CONTEXT_BUTTON_TRIGGERED";
    public static const SELL_BUTTON_TRIGGERED:String = "WALL_OBJECT_SELL_BUTTON_TRIGGERED";
    public static const MOVE_BUTTON_TRIGGERED:String = "WALL_OBJECT_MOVE_BUTTON_TRIGGERED";
    private var _backgroundImage:Scale9Image;
    private var _deployed:Boolean = false;

    public function WallObjectContextMenu() {
        this._backgroundImage = new Scale9Image(new Scale9Textures(GraphicFactory.getImageForRender("Interface/Skins/invisible_square").texture,new Rectangle(10,10,1,1)));
        this._backgroundImage.width = 100;
        this._backgroundImage.height = 70;
        this._backgroundImage.y = 15;
        this._backgroundImage.alignPivot();
        this.addChild(this._backgroundImage);

        this._sellButton = new ButtonSmall(ButtonSmall.ORANGE_TYPE,ButtonSmall.SIGN_CROSS);
        this._sellButton.addEventListener(Event.TRIGGERED, HANDLER_sellTriggered);
        this.addChild(this._sellButton);

        this._moveButton = new ButtonSmall(ButtonSmall.ORANGE_TYPE,ButtonSmall.SIGN_CROSS);
        this._moveButton.addEventListener(Event.TRIGGERED, HANDLER_moveTriggered);
        this.addChild(this._moveButton);

        this._sellButton.x = this._moveButton.x = -48/2;
        this._sellButton.y = 30;
    }
    public function animateAppearing():void {
        this._sellButton.y = 0;
        this._moveButton.y = -30;
        this.touchable = false;
        this._deployed = true;
        TweenMax.killTweensOf(this._sellButton);
        TweenMax.killTweensOf(this._moveButton);
        TweenMax.to(this._moveButton,0.5,{y:-20,alpha:1, ease:Back.easeOut});
        TweenMax.to(this._sellButton,0.5,{y:30,alpha:1});
        TweenMax.delayedCall(0.3, _setTouchable);
        //TweenMax.to(this, 2.5, {touchable:true});
    }

    private function _setTouchable():void {
        this.touchable = true;
    }

    public function animateDisappearing():void {
        this.touchable = false;
        this._deployed = false;
        TweenMax.killTweensOf(this._sellButton);
        TweenMax.killTweensOf(this._moveButton);
        TweenMax.to(this._moveButton,0.2,{y:-30,alpha:0,useFrames:false});
        TweenMax.to(this._sellButton,0.2,{y:0,alpha:0, ease:Quad.easeIn,useFrames:false, onComplete:this.removeFromParent});
    }

    private function HANDLER_sellTriggered(event:Event):void {
        dispatchEventWith(EVENT_CONTEXT_BUTTON_TRIGGERED, false, {type: SELL_BUTTON_TRIGGERED});
    }
    private function HANDLER_moveTriggered(event:Event):void {
        dispatchEventWith(EVENT_CONTEXT_BUTTON_TRIGGERED, false, {type: MOVE_BUTTON_TRIGGERED});
    }
    public function set position(point:Point):void {
        this.x = point.x;
        this.y = point.y;
    }
}
}
